﻿using Microsoft.Win32;
using SmartPlant.WinCore.Helper;
using System;
using System.Globalization;

namespace SmartPlant.Updater
{
    /// <summary>
    /// 提供一种基于在Windows注册表上存储数据在会话之间存储AutoUpdater状态的机制。
    /// </summary>
    public class RegistryPersistenceProvider : IPersistenceProvider
    {
        /// <summary>
        /// 获取/设置将包含数据的Windows注册表项的路径。
        /// </summary>
        private string RegistryLocation { get; }

        private const string RemindLaterValueName = "RemindLaterAt";

        private const string SkippedVersionValueName = "SkippedVersion";

        /// <summary>
        /// 初始化<see cref="RegistryPersistenceProvider"/>类的新实例，该类指示用于存储数据的Windows注册表项的路径。
        /// </summary>
        /// <param name="registryLocation"></param>
        public RegistryPersistenceProvider(string registryLocation)
        {
            RegistryLocation = registryLocation;
        }

        /// <inheritdoc />
        public Version GetSkippedVersion()
        {
            try
            {
                using RegistryKey updateKey = Registry.CurrentUser.OpenSubKey(RegistryLocation);
                object skippedVersionValue = updateKey?.GetValue(SkippedVersionValueName);

                if (skippedVersionValue != null)
                {
                    return new Version(skippedVersionValue.ToString());
                }
            }
            catch (Exception x)
            {
                LoggerHelper.Current.Error(x.Message, x);
            }

            return null;
        }


        /// <inheritdoc />
        public DateTime? GetRemindLater()
        {
            using RegistryKey updateKey = Registry.CurrentUser.OpenSubKey(RegistryLocation);
            object remindLaterValue = updateKey?.GetValue(RemindLaterValueName);

            if (remindLaterValue != null)
            {
                try
                {
                    return Convert.ToDateTime(remindLaterValue.ToString(),
                        CultureInfo.CreateSpecificCulture("en-US").DateTimeFormat);
                }
                catch (FormatException x)
                {
                    LoggerHelper.Current.Error(x.Message, x);
                }
            }

            return null;
        }

        /// <inheritdoc />
        public void SetSkippedVersion(Version version)
        {
            using (RegistryKey autoUpdaterKey = Registry.CurrentUser.CreateSubKey(RegistryLocation))
            {
                autoUpdaterKey?.SetValue(SkippedVersionValueName, version != null ? version.ToString() : string.Empty);
            }
        }

        /// <inheritdoc />
        public void SetRemindLater(DateTime? remindLaterAt)
        {
            using (RegistryKey autoUpdaterKey = Registry.CurrentUser.CreateSubKey(RegistryLocation))
            {
                autoUpdaterKey?.SetValue(RemindLaterValueName,
                    remindLaterAt != null
                        ? remindLaterAt.Value.ToString(CultureInfo.CreateSpecificCulture("en-US").DateTimeFormat)
                        : string.Empty);
            }
        }
    }
}